package top.itaso.app.ui.picker;

import androidx.appcompat.app.AlertDialog;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

import top.itaso.app.R;
import top.itaso.app.databinding.FragmentPickerBinding;
import top.itaso.lib.alog.ALog;
import top.itaso.lib.widget.AutoFilterInputEditView;
import top.itaso.lib.widget.WidgetDateTimePicker;

public class PickerFragment extends Fragment {
    private static final String TAG = "PickerFragment";
    private PickerViewModel mViewModel;
    private FragmentPickerBinding binding;

    private TextView tvStartDateMonth;
    private TextView tvEndDateMonth;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mViewModel = new ViewModelProvider(this).get(PickerViewModel.class);
        binding = FragmentPickerBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        initView(root);

        binding.layoutFilterLevel1.getResultNode().observe(getViewLifecycleOwner(), new Observer<AutoFilterInputEditView.Node>() {
            @Override
            public void onChanged(AutoFilterInputEditView.Node node) {

                ArrayList<AutoFilterInputEditView.Node> nodes = (ArrayList<AutoFilterInputEditView.Node>) node.getChildren();
                if (nodes != null && !nodes.isEmpty()) {
                    binding.layoutFilterLevel2.updateNodes(nodes);
                }
            }
        });
        binding.layoutFilterLevel1.updateNodes(mViewModel.getLevelNode(mViewModel.readOrgMemberFromExcel(), 1));
        return root;
    }

    private void initView(View rootView) {
        initDateTimeView(rootView);
    }

    private void initDateTimeView(View rootView) {
        tvStartDateMonth = rootView.findViewById(R.id.tv_start_date_month);
        tvEndDateMonth = rootView.findViewById(R.id.tv_end_month);

        Drawable drawable = getResources().getDrawable(R.mipmap.ic_calendar_disabled).mutate();
        drawable = tintDrawable(drawable, getResources().getColor(R.color.color_text));
        tvStartDateMonth.setBackground(drawable);
        tvEndDateMonth.setBackground(drawable);

        View layoutStartDate = rootView.findViewById(R.id.layout_start_date);
        layoutStartDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPickerNumberPicker(binding.tvStartDate);
            }
        });
        View layoutEndDate = rootView.findViewById(R.id.layout_end_date);
        layoutEndDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPickerNumberPicker(binding.tvEndDate);
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    //着色
    private Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
        final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(wrappedDrawable, colors);
        return wrappedDrawable;
    }

    //着色
    private Drawable tintDrawable(Drawable drawable, int color) {
        final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(wrappedDrawable, color);
        return wrappedDrawable;
    }


    /**
     * 承载 使用 NumberPicker 原生控件实现的 PickerNumberDateTimeView 视图
     *
     * @param anchor
     */
    private void showPickerNumberPicker(View anchor) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("请选择时间");

        WidgetDateTimePicker widgetDatePicker = new WidgetDateTimePicker(getContext());
        builder.setView(widgetDatePicker);

        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                widgetDatePicker.onConfirm(new WidgetDateTimePicker.IDateCallback() {
                    @Override
                    public void onConfirm(String dateValue) {
                        ALog.dTag(TAG, "dateValue:%s", dateValue);
                        if (anchor instanceof TextView) {
                            ((TextView) anchor).setText(dateValue);
                        } else if (anchor instanceof EditText) {
                            ((EditText) anchor).setText(dateValue);
                        } else {

                        }
                    }
                });
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create().show();
    }
}